﻿using System;
using System.Reflection;
using Inst = Bettzueche.RLGlue.Codec.Installer;

namespace Bettzueche.RLGlue.Codec {

    /// <summary>
    /// "This is the executable class from the JavaRLGlueCodec JAR."
    /// <para>"It can be used to load agents and environments (although that approach is no longer the best way :: see Agent/Environment 
    /// Loading in the Java Codec Manual)."</para><para>
    /// "It will also tell you exactly what version and build of the codec you are using if you
    /// do<br />java -jar JavaRLGlueCodec --version " (Brian Tanner)</para>
    /// </summary>
    public class RLGlueCore {

        private static void printDiagnostics(){
            Console.WriteLine("--------------------------------");
            Console.WriteLine("RL-Glue Java Codec");
            Console.WriteLine("--------------------------------");
            Console.WriteLine("\t\tInterface Version: "+GetSpecVersion());
            Console.WriteLine("\t\tBuild Version: "+GetImplementationVersion());

            bool isInstalled = Inst.Installer.IsInstalled();

            if(isInstalled){
                Console.WriteLine("Installed in: "+ Inst.Installer.GetInstallDir());
            }else{
                Console.WriteLine("NOT Installed: free-floating at: "+ Inst.Installer.GetFloatDir());
            }

        }
        private static void printHelp() {
            Console.WriteLine("--------------------------------");
            Console.WriteLine("RL-Glue.jar main diagnostic program");
            Console.WriteLine("--------------------------------");
            Console.WriteLine("-v or --version will print out the interface version");
            Console.WriteLine("-b or --buildversion will print out the build version");
            Console.WriteLine("--install install the RL-Glue Java Extension into a system extensions directory");
            Console.WriteLine("--uninstall remove the RL-Glue Java Extension from the system extensions directory");
        }

        private static void printVersion() {
            Console.WriteLine(GetSpecVersion());
        }
        private static void printBuildVersion() {
            Console.WriteLine(GetImplementationVersion());
        }
        /*
         * Print out the current interface version of RLGlue.  We'll make it do more interesting things with commandline parameters later
         */

        static void Main(string[] args) {
            if (args.Length == 0) {
                printDiagnostics();
                printHelp();
                return;
            }
            if (String.Equals(args[0], "-v", StringComparison.InvariantCultureIgnoreCase) ||
                String.Equals(args[0], "--version", StringComparison.InvariantCultureIgnoreCase)) {
                printVersion();
                return;
            }
            if (args[0].Equals("-b", StringComparison.InvariantCultureIgnoreCase) ||
                args[0].Equals("--buildversion", StringComparison.InvariantCultureIgnoreCase)) {
                printBuildVersion();
                return;
            }

            if (args[0].Equals("--install", StringComparison.InvariantCultureIgnoreCase)) {
                Inst.Installer.Install();
            }
            if (args[0].Equals("--uninstall", StringComparison.InvariantCultureIgnoreCase)) {
                Inst.Installer.Uninstall();
            }
        }


        /**
         * Get the Specification (Interface) version of RLGlue as set in the Manifest file.  
         * @return String representation of current RLGlue Interface version.
         * @since 2.1
         * 
         */
        public static String GetSpecVersion() {
            return Properties.Settings.Default.RLGlueSpecVersion;
            //String specAsString = RLGlueCore.class.getPackage().getSpecificationVersion();
            //return specAsString;RLGlueSpecVersion
        }

        /**
         * Get the Implementation (Build) version of RLGlue as set in the Manifest file.  
         * @return String representation of current RLGlue Build version.
         * @since 2.1
         */
        public static String GetImplementationVersion() {
            return Assembly.GetEntryAssembly().GetName().Version.ToString();
        }
    
    
    }
}
